package com.qjdchina.sku.biz.impl;

import com.qjdchina.commons.enums.IsDeleted;
import com.qjdchina.commons.model.PageModel;
import com.qjdchina.commons.result.Result;
import com.qjdchina.sku.biz.CaseStoreInfoManager;
import com.qjdchina.sku.converter.CaseStoreInfoConverter;
import com.qjdchina.sku.dao.mapper.CaseStoreInfoMapper;
import com.qjdchina.sku.model.CaseStoreInfo;
import com.qjdchina.sku.model.vo.CaseStoreInfoVO;
import com.qjdchina.sku.service.enums.SkuResultCode;
import com.qjdchina.sku.util.DateUtil;
import com.qjdchina.sku.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Luoyan on 2017/6/11.
 */
@Service
public class CaseStoreInfoManagerImpl implements CaseStoreInfoManager {

    @Autowired
    private CaseStoreInfoMapper caseStoreInfoMapper;

    @Override
    public Result<PageModel<CaseStoreInfoVO>> queryPagedCaseStoreInfoList(Map<String, Object> params,
                                                                          PageModel<CaseStoreInfoVO> pageModel) {
        int pagedCaseStoreInfoTotal = caseStoreInfoMapper.getPagedCaseStoreInfoTotal(params);
        List<CaseStoreInfo> caseStoreInfoList = caseStoreInfoMapper.queryPagedCaseStoreInfo(params, pageModel);
        List<CaseStoreInfoVO> caseStoreInfoVOList = new ArrayList<CaseStoreInfoVO>();
        if (caseStoreInfoList != null && caseStoreInfoList.size() > 0) {
            for (CaseStoreInfo caseStoreInfo : caseStoreInfoList) {
                CaseStoreInfoVO caseStoreInfoVO = CaseStoreInfoConverter.toCaseStoreInfoVO(caseStoreInfo);
                String[] arr = caseStoreInfo.getPicturePath().split(",");
                List<String> list = Arrays.asList(arr);
                caseStoreInfoVO.setPicturePathList(list);
                caseStoreInfoVOList.add(caseStoreInfoVO);
            }
        }
        pageModel.setPagedRecords(caseStoreInfoVOList);
        pageModel.setTotalCount(pagedCaseStoreInfoTotal);
        return new Result<PageModel<CaseStoreInfoVO>>(pageModel);
    }

    @Override
    public Result<CaseStoreInfoVO> queryCaseStoreInfoVODetail(Integer caseStoreInfoId) {
        if (caseStoreInfoId == null) {
            return new Result<CaseStoreInfoVO>(SkuResultCode.PARAMETER_NOT_EMPTY);
        }

        CaseStoreInfo caseStoreInfo = caseStoreInfoMapper.selectByPrimaryKey(caseStoreInfoId);
        if (caseStoreInfo == null) {
            return new Result<CaseStoreInfoVO>(SkuResultCode.DATA_NOT_EXIST);
        }
        CaseStoreInfoVO caseStoreInfoVO = CaseStoreInfoConverter.toCaseStoreInfoVO(caseStoreInfo);
        String[] arr = caseStoreInfo.getPicturePath().split(",");
        List<String> list = Arrays.asList(arr);
        caseStoreInfoVO.setPicturePathList(list);

        return new Result<CaseStoreInfoVO>(caseStoreInfoVO);
    }

    @Override
    public Result<Integer> createCaseStoreInfo(CaseStoreInfoVO caseStoreInfoVO) {
        caseStoreInfoVO.setIsDeleted(IsDeleted.NO.getValue());
        caseStoreInfoVO.setGmtCreated(new Date());
        caseStoreInfoVO.setGmtModified(new Date());

        CaseStoreInfo caseStoreInfo = CaseStoreInfoConverter.toCaseStoreInfo(caseStoreInfoVO);
        caseStoreInfo.setProjectDate(DateUtil.shortStrToDate(caseStoreInfoVO.getProjectDateStr()));
        caseStoreInfo.setPicturePath(StringUtil.listToString(caseStoreInfoVO.getPicturePathList(), ','));

        int row = caseStoreInfoMapper.insertSelective(caseStoreInfo);
        if (row == 0) {
            return new Result<Integer>(SkuResultCode.ADD_FAIL);
        }

        return new Result<Integer>(caseStoreInfo.getId());
    }
}
